<?php
/**
 * 商城家里管理
 * author:ty
 * date:2017/08/22
 */
namespace App\Http\Controllers;

use App\Http\Models\Fitment;
use App\Http\Models\Shopcart;
use App\Http\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
class FitmentController extends Controller
{

    public $fitment = null;
    public $color = null;//公用颜色配置
    public function __construct()
    {
        $this->nav = '家具租赁';
        $this->fitment = new Fitment();
        $this->shopcart = new Shopcart();
        $this->color = CommonController::commmon_color_path();
    }

    /**家具列表页*/
    public function index()
    {
        if(!session('user')){
            $this->user = '';
        }else{
            $this->user = User::find(session('user')->id);
            $param['cart'] = CommonController::shopcart(session('user')->id);
        }
        /**页面传参*/
        $param['fitment_brand'] = trim(Input::get('fitment_brand',''));//品牌
        $param['fitment_type_classify'] = trim(Input::get('fitment_type_classify',''));//分类【办公，商用，居家】
        $param['fitment_subcate_id']= trim(Input::get('fitment_subcate_id',''));//家具子分类
        $param['fitment_price']= trim(Input::get('fitment_price',''));//租赁价格
        $param['minprice'] = trim(Input::get('minprice',''));
        $param['maxprice'] = trim(Input::get('maxprice',''));
        $param['fitment_type'] = trim(Input::get('fitment_type',''));//新旧
        $param['fitment_rent'] = trim(Input::get('fitment_rent',1));//租赁方式
        $param['fitment_orderby'] = trim(Input::get('fitment_orderby',''));//排序
        $param['fitment_name'] = trim(Input::get('fitment_name',''));//家具名
        $param['per_page'] = 9;
        $param['current_page'] = 1;
        /**品牌*/
        $param['brand'] = $this->fitment->brand();
        /**子分类列表*/
        $param['classify_lists'] = DB::table('subcate')->where('subcate_pid',$param['fitment_type_classify'])->get();
        /**家具列表*/
        $param['data'] = $this->fitment->lists($param);
        /***颜色*/
        $param['color'] = $this->color;
      
        return view('fitment.list',['user'=>$this->user,'nav'=>$this->nav,'param'=>$param]);
    }

    /**家具详情*/
    public function show($id)
    {
        if(!session('user')){
            $this->user = '';
        }else{
            $this->user = User::find(session('user')->id);
            $param['cart'] = CommonController::shopcart(session('user')->id);
        }
        /**用户信用额度*/
        $param['user_quota'] = DB::table('quota')->where('quota_user_id',session('user')->id)->first();

        $param['current_id'] = $id;
        /**默认显示*/
        $param['current'] = $this->fitment->show($id);
        /**颜色还规格*/
        $param['detail_colors']=$this->fitment->fitment_colors($id);
        /**家具规格*/
        $param['details'] = $this->fitment->details($id);
        /**组合家具*/
        $param['combinedata'] = $this->fitment->combine();
        if(count($param['combinedata'])>0){
            $resda = array();
            foreach ($param['combinedata'] as $k=>$v)
            {
                if(in_array($id,$v['combine_id'])){
                    $resda[$v['price']]= $v['combine_id'];
                }
            }
            $combinedt = array();
            if(count($resda)>0)
            { //有组合数据
                ksort($resda);
                foreach ($resda as $k=>$v){
                    $combinedt[] = $v;
                }
                $param['combine'] = $this->fitment->combinedet($combinedt[0]);
            } else { //没组合数据
                foreach ($param['combinedata'] as $k=>$v){
                    $combinedt[] = $v;
                }
                $param['combine'] = $this->fitment->combinedet($combinedt[0]['combine_id']);
            }
        }
        $param['color'] = $this->color;
        return view('fitment.detail',['user'=>$this->user,'nav'=>$this->nav,'param'=>$param]);
    }

    /**数据加载查询*/
    public function append()
    {
        $param['fitment_brand'] = trim(Input::get('fitment_brand',''));//品牌
        $param['fitment_type_classify'] = trim(Input::get('fitment_type_classify',''));//分类【办公，商用，居家】
        $param['fitment_subcate_id']= trim(Input::get('fitment_subcate_id',''));//子分类id
        $param['fitment_price']= trim(Input::get('fitment_price',''));//租赁价格
        $param['minprice'] = trim(Input::get('minprice',''));
        $param['maxprice'] = trim(Input::get('maxprice',''));
        $param['fitment_type'] = trim(Input::get('fitment_type',''));//新旧
        $param['fitment_rent'] = trim(Input::get('fitment_rent',1));//租赁方式
        $param['fitment_orderby'] = trim(Input::get('fitment_orderby',''));//排序
        $param['fitment_name'] = trim(Input::get('fitment_name',''));//家具名
        $param['per_page'] = 9;//每次查询数据条数
        $param['current_page'] = trim(Input::get('fitment_currentpage',1));//当前查询页数

        /**家具列表*/
        $param['data'] = $this->fitment->lists($param);
        /***颜色*/
        $param['color'] = $this->color;

        if(count($param['data'])>0){
            $result =['res'=>'ok','msg'=>$param['data'],'color'=>$param['color'],'filepath'=>env('STATIC_FILE_ERP_PATH','http://115.159.95.113:8080/')];
            echo json_encode($result);
        }else{
            $result =['res'=>'error','msg'=>'只有这么多数据了!','data'=>$param['data']];
            echo json_encode($result);
        }
    }

}